
/*
   tsAPI Distribution
   spin_ScrollBar Class

   TrueSpectra Inc, 2002
   Code by: Kevin Martin
*/


function spin_ScrollBar(orientation,totFrames,startFrame,watchmouseOver,imgFolder,zoomWindow) {
	this.knob_min = 40;

	this.isBinDesc = false;

	this.zoomWindow = zoomWindow || null;

	this.totFrames = Math.max(1,totFrames);
	this.startFrame = Math.max(1,Math.min(this.totFrames,startFrame));
	this.delay = 100;
	this.writedelay = 200;
	this.curload = 0;
	this.spin_Array = new Array();
	this.preLoadimg = new Array();

	this.replaceRules = new Array();
	this.curframe = this.startFrame;
	this.targetImagesLow = new Array();
	this.targetImagesHigh = new Array();
	this.targetImagesHighVisible = new Array();

	this.isSpin = false;

	this.DynLayer = DynLayer;
	this.DynLayer(null,0,0,0,0);

	this.scroll_bar = new ts_ScrollBar(ts_ScrollBar.HORIZONTAL,0,0,watchmouseOver,imgFolder);

	this.scroll_bar = this.addChild( this.scroll_bar );

	this.or = orientation;

	var vlistener = new EventListener(this);
	vlistener.onscrollStop = function(e) { var o = e.getTarget(); 
		o.changeFrame(o.scroll_bar.getPos(),true);  
	};
	vlistener.onscroll = function(e)     { var o = e.getTarget(); o.changeFrame(o.scroll_bar.getPos(),false); };
	this.scroll_bar.addEventListener(vlistener);

	var resizeEvents = new EventListener(this);
	resizeEvents.onresize = function(e) {
		var o = e.getTarget();
		o.scroll_bar.setSize( o.w,o.h );
		o.scroll_bar.setPos( Math.round( o.scroll_bar.getMaxPos()/o.totFrames*(o.curframe-0.5) ) );
	}; 
	this.addEventListener(resizeEvents);

	this.setOrientation(orientation);
};

spin_ScrollBar.prototype = new DynLayer;

spin_ScrollBar.prototype.reset = function() {
	this.scroll_bar.reset();
};

spin_ScrollBar.prototype.setOrientation = function(type) { // 0=horz,1=vert
	this.horizontal = (type==spin_ScrollBar.HORIZONTAL);
	this.vertical = (type==spin_ScrollBar.VERTICAL);
};


spin_ScrollBar.prototype.addTargetImage = function( o,hires ) {
	if( hires==true ) 	{ this.targetImagesHigh[this.targetImagesHigh.length] = o; }
	else			{ this.targetImagesLow[this.targetImagesLow.length] = o; };
};

spin_ScrollBar.VERTICAL = 1;
spin_ScrollBar.HORIZONTAL = 2;

spin_ScrollBar.prototype.changeUI = function(imgFolder) {
  this.scroll_bar.changeUI(imgFolder);
};

spin_ScrollBar.prototype.replSPINFrame = function(str,sPre,sPost,sframe) {
  i = 0;
  while(1) {
    if( (i2=str.indexOf(sPre,i)) >= 0 ) {
      if( (i3=str.indexOf(sPost,i2)) >=0 ) {
        str = str.substring(0,i2)+sPre+sframe+str.substring(i3);
        i = i3+sPost.length;
      } else {
        i = i2+sPre.length;
      };
    } else {
      break;
    };
  };
  return str;
};

spin_ScrollBar.prototype.replSPINFrameCNo = function(str,sPre,num,sframe) {
  i = 0;
  while(1) {
    if( (i2=str.indexOf(sPre,i)) >= 0 ) {
        if( !isNaN( str.substring(i2+sPre.length,i2+sPre.length+num) ) ) {
          strframe = ""+sframe;
	  while( strframe.length < num ) { strframe = "0"+strframe; };
          str = str.substring(0,i2)+sPre+strframe+str.substring(i2+sPre.length+num);
          i = i2+sPre.length+num;
        } else {
          i = i2+sPre.length;
        };
    } else {
      break;
    };
  };
  return str;
};

spin_ScrollBar.prototype.addReplaceRule1 = function( pre, post, inc ) {
  this.replaceRules[this.replaceRules.length] = "this.replSPINFrame(sURL,'"   + pre +  "','"  + post +  "',(''+("+(isNaN(inc)?1:Math.max(1,inc))+"*iframe)))";
};
spin_ScrollBar.prototype.addReplaceRule2 = function( pre, num, inc ) {
  this.replaceRules[this.replaceRules.length] = "this.replSPINFrameCNo(sURL,'"+pre+"',"+num+",(''+("+(isNaN(inc)?1:Math.max(1,inc))+"*iframe)))";
};

spin_ScrollBar.prototype.applyRules = function( sURL, iframe ) {
	for( var i=0;i<this.replaceRules.length; i++ ) {
		sURL =  eval(this.replaceRules[i]);
	};
	return sURL;
};

spin_ScrollBar.prototype.spin = function( frames ) {
	this.scroll_bar.setPos( ((this.scroll_bar.getMaxPos()*2)+this.scroll_bar.getPos()+Math.round(this.scroll_bar.getMaxPos()/this.curload)*frames)% this.scroll_bar.getMaxPos() );
	this.changeFrame(this.scroll_bar.getPos(),false);  
	this.changeFrame(this.scroll_bar.getPos(),true);  
};

spin_ScrollBar.prototype.gotoFrame = function( frameNum,finalUpdate ) {
	this.scroll_bar.setPos( parseInt(this.scroll_bar.getMaxPos()/this.curload)*Math.min(this.curload,frameNum) );
	this.changeFrame(this.scroll_bar.getPos(),false);  
	if( finalUpdate==true ) this.changeFrame(this.scroll_bar.getPos(),true);  
};

spin_ScrollBar.prototype.changeFrame = function( v, finalUpdate ) {
	var position = 1;

	if( this.scroll_bar.getMaxPos() > 0 ) { position = Math.round( v*(this.curload+1)/this.scroll_bar.getMaxPos() )+1; };

	if( position > this.totFrames )	{ position = this.totFrames; };

	this.curframe = parseInt(this.spin_Array[position-1]);
	if( finalUpdate==true ) { 

		for( var i=0;i<this.targetImagesHigh.length; i++ ) {
			if( this.targetImagesHigh[i].setImageSpecial )
				this.targetImagesHigh[i].setImageSpecial( this.applyRules( this.targetImagesHigh[i].getImageSrc(),this.curframe) );
			else
				this.targetImagesHigh[i].setImageSrc( this.applyRules( this.targetImagesHigh[i].getImageSrc(),this.curframe) );
		};
		this.isSpin = false;

	} else {

		if( this.isSpin == false ) {
			this.isSpin = true;
			for( var i=0;i<this.targetImagesHigh.length; i++ ) {
				this.targetImagesHighVisible[i] = this.targetImagesHigh[i].getVisible();
				this.targetImagesHigh[i].setVisible(false);
			};
		};

		for( var i=0;i<this.targetImagesLow.length; i++ ) {
			this.targetImagesLow[i].setImageSrc( this.applyRules( this.targetImagesLow[i].getImageSrc(),this.curframe) );
		};
	};

};


/**************PRELOADING IMAGES CODE********************/

spin_ScrollBar.prototype.getPos = function() {		return this.scroll_bar.getPos(); };
spin_ScrollBar.prototype.getMaxPos = function() {	return this.scroll_bar.getMaxPos(); };
spin_ScrollBar.prototype.setPos = function(i) {		this.scroll_bar.setPos(i); };

spin_ScrollBar.prototype.start_loadLowRes = function() { 
	if( this.zoomWindow != null ) this.zoomWindow.enableWaitIcon("Loading 0%...");
	this.curload=0; 
	if( this.isBinDesc==true )	this.binDesc_createLoadOrder();
	else				this.regDesc_createLoadOrder();

	this.load_LowRes_Images(0); 
};

spin_ScrollBar.prototype.load_LowRes_Images = function(num) {
        if( this.curload > this.totFrames || num >= this.targetImagesLow.length ) { alert( "error"+(num >= this.targetImagesLow.length ) ); };

	this.zoomWindow.enableWaitIcon("Loading "+Math.floor((this.curload+1)*100/this.totFrames)+"%...");

        var str = this.applyRules(this.targetImagesLow[num].getImageSrc(),parseInt(this.spin_Array[this.curload]) );
	this.preLoadimg[this.curload] = DynImage.getImage(str);
	setTimeout( this+'.check_loadLowRes('+this.curload+','+num+')',this.delay );
};

spin_ScrollBar.prototype.check_loadLowRes = function(i,num) {
	if( this.preLoadimg[i].width < 2 ) {
		setTimeout( this+'.check_loadLowRes('+i+','+num+')', this.delay );
		return;
	} else {
		if( num < this.targetImagesLow.length-1 ) {
			this.load_LowRes_Images( (num+1) );
		} else {
			if( this.curload < this.totFrames )	{
				this.curload++;
				if( this.isBinDesc!=true ) this.gotoFrame(this.curload,false);		
				if( this.curload < this.totFrames ) { 
					this.load_LowRes_Images( 0 );
				} else {

					if( this.isBinDesc!=true ) this.gotoFrame(0,true)
					if( this.zoomWindow != null ) this.zoomWindow.disableWaitIcon();
				};
				if( this.isBinDesc==true ) this.binDesc_reOrder(); 
			};
		};
	};


};





/**********************BINARY DESCENT*************************/
/*************************************************************/

spin_ScrollBar.prototype.binDesc_findFrame = function(num) {
  for(var i=0;i<this.curload;i++) {
    if( parseInt(this.spin_Array[i]) >= num ) { return i; };
  };
  return this.curload;
};

spin_ScrollBar.prototype.binDesc_reOrder = function() {
  if( this.curload <= this.totFrames ) {			//Sanity check
    for( var i=this.curload;i>0;i-- ) {
      if( parseInt(this.spin_Array[i]) < parseInt(this.spin_Array[i-1]) ) {
        var temp = this.spin_Array[i];
        this.spin_Array[i] = this.spin_Array[i-1];
        this.spin_Array[i-1] = temp;
      } else {
        break;
      };
    };
  } else {
     alert( "Error with Frame Table size" );
  };
};

spin_ScrollBar.prototype.regDesc_createLoadOrder = function() {
  var regDesc_List = "";
  for( var i=1;i<=this.totFrames;i++ ) {
    regDesc_List = regDesc_List+i+(i!=this.totFrames?",":"");
  }
  this.spin_Array = regDesc_List.split(",");
};

spin_ScrollBar.prototype.binDesc_createLoadOrder = function() {
  var num = this.totFrames;
  var start = this.curframe;
  var offset = 0;
  var series = 0;
  var sNum = 0;

  var binDesc_List = "";

  while( num > (Math.pow(2,series)-1) ) { series++; };

  offset = Math.round(((Math.pow(2,series)-1)-num)/2);
  sNum = (Math.pow(2,series)-1);
  if( start != 0 ) { start = ( (start-1) - (parseInt((sNum+1)/2+0.5)-offset) + num*10) % num; };

  for( var i=0; i<series; i++ ) {
    var cur = parseInt( (sNum/Math.pow(2,i) )+1 );

    for( var i2=0;i2<Math.pow(2,i);i2++ ) {
      var curNum = (parseInt((cur/2)+0.5)+cur*i2)-offset;
      if( curNum > 0 && curNum <= num ) { 
        if( start != 0 ) { curNum = ((curNum+start) % num)+1; };
	binDesc_List = binDesc_List+","+curNum;				//Create Text List
      };
    };
  };
  if(binDesc_List.length > 1) { binDesc_List=binDesc_List.substring(1); };
  this.spin_Array = binDesc_List.split(",");
};
